package com.ydh.company.filter;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @Author:wkl
 * @Date: 2020/4/23 22:28
 * @Version 0.0.1-SNAPSHOT
 */
public class RolesOrFilter extends AuthorizationFilter {


    @Override protected boolean isAccessAllowed(ServletRequest servletRequest,
                                                ServletResponse servletResponse,
                                                Object o) throws Exception {
        //1.获取主体
        Subject subject = getSubject(servletRequest,servletResponse);
        //2.获取角色数据
        String[] roles = (String[]) o;
        //判断roles
        if(roles == null || roles.length == 0){
            //说明不需要任何的角色就可以直接访问
            return true;
        }
        //遍历
        for (String role : roles) {
            if (subject.hasRole(role)) {
                //只要拥有其中的一个就可以直接访问
                return true;
            }
        }
        //表示没有对应的访问权限
        return false;
    }
}
